﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Core.Objects.DataClasses;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Windows.Forms;
using System.Xml.Linq;
using BreakingHubAdmin.Services;
using DataMongo;
using DataMongo.Entities;
using WebMatrix.WebData;


namespace BreakingHubAdmin.Controllers
{
    public class SuperAdminController : Controller
    {
        //
        // GET: /SuperAdmin/
        public ActionResult Index()
        {
            if (WebSecurity.IsAuthenticated)
            {
                return View();
            }
            return RedirectToAction("LogOnSuperAdmin", "SuperAdmin");

        }

        

        public ActionResult LogOnSuperAdmin()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOnSuperAdmin(string Username, string Password)
        {
            if (Username != "" && Password != "")
            {
                if (Username == "admin" && Password == "P@ssw0rd")
                {
                    WebSecurity.Login(Username, Password, persistCookie: true);
                    return RedirectToAction("Index");
                }
            }
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View();
        }
        

        public ActionResult LogOff()
        {
            WebSecurity.Logout();

            return RedirectToAction("Index");
        }

          #region Category

        public ActionResult Category()
        {
            var connection = new RepositoryBaseMongo("Category");
            var data = connection.GetAll<Category>();
            return View(data);
        }

        public ActionResult CategoryAdd()
        {
            var connection = new RepositoryBaseMongo("Category");
            var category = new Category();
            category.CategoryList = new List<SelectListItem>();
            var data = connection.GetAll<Category>();
            category.CategoryList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
            category.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(category);
        }

        [HttpPost]
        public ActionResult CategoryAdd(Category category)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Category");
                int Id = 0;
                var categories = connection.GetAll<Category>().ToList();
                if (categories.Any())
                    Id = categories.Max(x => x.CategoryId);
                Id += 1;
                category.CategoryId = Id;
                category.IsActive = true;
                connection.InsertOrUpdate(category);
                return RedirectToAction("Category");
            }
            return null;
        }

        public ActionResult CategoryEdit(int id)
        {

            var connection = new RepositoryBaseMongo("Category");
            List<Category> list = (from f in connection.GetAll<Category>()
                                   where f.CategoryId == id
                                   select f).ToList();
            Category category = new Category();
            if (list.Count > 0) category = list[0];
            var data = connection.GetAll<Category>();
            category.CategoryList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
            category.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(category);
        }

        [HttpPost]
        public ActionResult CategoryEdit(Category category)
        {
            var connection = new RepositoryBaseMongo("Category");
            List<Category> list = (from f in connection.GetAll<Category>()
                                   where f.CategoryId == category.CategoryId
                                   select f).ToList();
            Category newcategory = new Category();
            if (list.Count > 0) newcategory = list[0];
            category._id = newcategory._id;
            connection.InsertOrUpdate(category);
            return RedirectToAction("Category");
        }

        public ActionResult CategoryDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Category");
            List<Category> list = (from f in connection.GetAll<Category>()
                                   where f.CategoryId == id
                                   select f).ToList();
            Category category = new Category();
            if (list.Count > 0) category = list[0];
            connection.Delete<Category>(category._id);
            return RedirectToAction("Category");
        }

        #endregion

        #region Source

        public ActionResult Source()
        {
            var connection = new RepositoryBaseMongo("Source");
            var data = connection.GetAll<Source>();
            return View(data);
        }

        public ActionResult SourceAdd()
        {
            var connection = new RepositoryBaseMongo("Source");
            var category = new Source();
            return View(category);
        }

        [HttpPost]
        public ActionResult SourceAdd(Source source)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Source");
                int Id = 0;
                var sources = connection.GetAll<Source>().ToList();
                if (sources.Any())
                    Id = sources.Max(x => x.SourceId);
                Id += 1;
                source.SourceId = Id;
                source.IsActive = true;
                connection.InsertOrUpdate(source);
                return RedirectToAction("Source");
            }
            return null;
        }

        public ActionResult SourceEdit(int id)
        {

            var connection = new RepositoryBaseMongo("Source");
            List<Source> list = (from f in connection.GetAll<Source>()
                                 where f.SourceId == id
                                 select f).ToList();
            Source sources = new Source();
            if (list.Count > 0) sources = list[0];

            return View(sources);
        }

        [HttpPost]
        public ActionResult SourceEdit(Source source)
        {
            var connection = new RepositoryBaseMongo("Source");
            List<Source> list = (from f in connection.GetAll<Source>()
                                 where f.SourceId == source.SourceId
                                 select f).ToList();
            Source newsource = new Source();
            if (list.Count > 0) newsource = list[0];
            source._id = newsource._id;
            connection.InsertOrUpdate(source);
            return RedirectToAction("Source");
        }

        public ActionResult SourceDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Source");
            List<Source> list = (from f in connection.GetAll<Source>()
                                 where f.SourceId == id
                                 select f).ToList();
            Source category = new Source();
            if (list.Count > 0) category = list[0];
            connection.Delete<Source>(category._id);
            return RedirectToAction("Source");
        }


        #endregion

        #region SourceDetails

        public ActionResult SourceDetail(int sourceid)
        {
            var connection = new RepositoryBaseMongo("SourceDetail");
            var data = connection.GetAll<SourceDetail>();

            List<SourceDetail> list = (from f in data
                                       where f.SourceId == sourceid
                                       select f).ToList();
            ViewBag.SourceId = sourceid;
            return View(list);
        }

        public ActionResult SourceDetailAdd(int sourceId)
        {
            var sourcedetail = new SourceDetail();
            sourcedetail.CategoryList = new List<SelectListItem>();
            var connectioncategory = new RepositoryBaseMongo("Category");
            var data = connectioncategory.GetAll<Category>();
            sourcedetail.CategoryList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
            sourcedetail.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));


            var connectionsource = new RepositoryBaseMongo("Source");
            var datasource = connectionsource.GetAll<Source>();

            List<Source> list = (from f in datasource
                                 where f.SourceId == sourceId
                                 select f).ToList();

            sourcedetail.SourceList = new List<SelectListItem>();
            sourcedetail.SourceList = new SelectList(list, "SourceId", "ShortDescription").ToList();
            ViewBag.SourceId = sourceId;
            return View(sourcedetail);
        }

        [HttpPost]
        public ActionResult SourceDetailAdd(SourceDetail sourceDetail)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("SourceDetail");
                int Id = 0;
                var sourcedetaillist = connection.GetAll<SourceDetail>().ToList();
                if (sourcedetaillist.Any())
                    Id = sourcedetaillist.Max(x => x.SourceDetailId);
                Id += 1;
                sourceDetail.SourceDetailId = Id;
                sourceDetail.IsActive = true;
                connection.InsertOrUpdate(sourceDetail);
                return RedirectToAction("SourceDetail", new { sourceid = sourceDetail.SourceId });
            }
            return null;
        }


        public ActionResult SourceDetailEdit(int id)
        {

            var connection = new RepositoryBaseMongo("SourceDetail");

            List<SourceDetail> list = (from f in connection.GetAll<SourceDetail>()
                                       where f.SourceDetailId == id
                                       select f).ToList();
            SourceDetail sourcedetail = new SourceDetail();
            if (list.Count > 0) sourcedetail = list[0];

            sourcedetail.CategoryList = new List<SelectListItem>();
            var connectioncategory = new RepositoryBaseMongo("Category");
            var datacategory = connectioncategory.GetAll<Category>();
            sourcedetail.CategoryList = new SelectList(datacategory, "CategoryId", "ShortDescription").ToList();
            sourcedetail.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));

            var connectionsource = new RepositoryBaseMongo("Source");
            var datasource = connectionsource.GetAll<Source>();
            List<Source> listsource = (from f in datasource
                                       where f.SourceId == sourcedetail.SourceId
                                       select f).ToList();
            sourcedetail.SourceList = new List<SelectListItem>();
            sourcedetail.SourceList = new SelectList(listsource, "SourceId", "ShortDescription").ToList();
            ViewBag.SourceId = sourcedetail.SourceId;
            return View(sourcedetail);

        }

        [HttpPost]
        public ActionResult SourceDetailEdit(SourceDetail sourcedetail)
        {
            var connection = new RepositoryBaseMongo("SourceDetail");
            List<SourceDetail> list = (from f in connection.GetAll<SourceDetail>()
                                       where f.SourceDetailId == sourcedetail.SourceDetailId
                                       select f).ToList();
            SourceDetail newsource = new SourceDetail();
            if (list.Count > 0) newsource = list[0];
            sourcedetail._id = newsource._id;
            connection.InsertOrUpdate(sourcedetail);
            return RedirectToAction("SourceDetail", new { sourceid = sourcedetail.SourceId });
        }

        public ActionResult SourceDetailDelete(int id)
        {
            var connection = new RepositoryBaseMongo("SourceDetail");
            List<SourceDetail> list = (from f in connection.GetAll<SourceDetail>()
                                       where f.SourceDetailId == id
                                       select f).ToList();
            SourceDetail sourcedetail = new SourceDetail();
            if (list.Count > 0) sourcedetail = list[0];
            connection.Delete<SourceDetail>(sourcedetail._id);
            return RedirectToAction("SourceDetail", new { sourceid = sourcedetail.SourceId });
        }

        #endregion

        #region FeedPosts

        public ActionResult FeedPost()
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            var data = connection.GetAll<FeedPost>();
            return View(data);
        }

        public ActionResult FeedPostAdd()
        {
            var connection = new RepositoryBaseMongo("Category");
            var feedpost = new FeedPost();
            feedpost.CategoryList = new List<SelectListItem>();
            var data = connection.GetAll<Category>();
            feedpost.CategoryList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
            feedpost.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            var connectionSource = new RepositoryBaseMongo("Source");
            feedpost.SourceList = new List<SelectListItem>();
            var sourcedata = connectionSource.GetAll<Source>();
            feedpost.SourceList = new SelectList(sourcedata, "SourceId", "ShortDescription").ToList();
            feedpost.SourceList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));

            return View(feedpost);
        }

        [HttpPost]
        public ActionResult FeedPostAdd(FeedPost model)
        {
            var Image1 = Request.Files[0];
            var Image2 = Request.Files[1];
            var connection = new RepositoryBaseMongo("FeedPost");
            if (Image1 != null && Image1.ContentLength > 0)
            {
                model.ImageURL1 = Path.GetFileName(Image1.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/FeedPost/"), model.ImageURL1);
                Image1.SaveAs(path);
            }

            if (Image2 != null && Image2.ContentLength > 0)
            {
                model.ImageURL2 = Path.GetFileName(Image2.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/FeedPost/"), model.ImageURL2);
                Image2.SaveAs(path);
            }
            int Id = 0;
            var Feedlist = connection.GetAll<FeedPost>().ToList();
            if (Feedlist.Any())
                Id = Feedlist.Max(x => x.FeedPostId);
            Id += 1;
            model.FeedPostId = Id;
            model.IsActive = true;
            connection.InsertOrUpdate(model);
            return RedirectToAction("FeedPost");
        }

        public ActionResult FeedPostEdit(int id)
        {
            var connection = new RepositoryBaseMongo("FeedPost");

            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == id
                                   select f).ToList();

            FeedPost feedpost = new FeedPost();
            if (list.Count > 0) feedpost = list[0];

            feedpost.CategoryList = new List<SelectListItem>();
            var connectioncategory = new RepositoryBaseMongo("Category");
            var datacategory = connectioncategory.GetAll<Category>();
            feedpost.CategoryList = new SelectList(datacategory, "CategoryId", "ShortDescription").ToList();
            feedpost.CategoryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));

            var connectionsource = new RepositoryBaseMongo("Source");
            var datasource = connectionsource.GetAll<Source>();
            feedpost.SourceList = new List<SelectListItem>();
            feedpost.SourceList = new SelectList(datasource, "SourceId", "ShortDescription").ToList();
            return View(feedpost);
        }

        [HttpPost]
        public ActionResult FeedPostEdit(FeedPost feedpost)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == feedpost.FeedPostId
                                   select f).ToList();
            FeedPost Newfeedpost = new FeedPost();
            if (list.Count > 0) Newfeedpost = list[0];
            feedpost._id = Newfeedpost._id;
            connection.InsertOrUpdate(feedpost);
            return RedirectToAction("FeedPost");
        }

        public ActionResult FeedPostDelete(int id)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == id
                                   select f).ToList();
            FeedPost feedpost = new FeedPost();
            if (list.Count > 0) feedpost = list[0];
            connection.Delete<SourceDetail>(feedpost._id);
            return RedirectToAction("FeedPost");
        }

        [HttpPost]
        public ActionResult UpdateUpcoming(int feedpostid)
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            List<FeedPost> list = (from f in connection.GetAll<FeedPost>()
                                   where f.FeedPostId == feedpostid
                                   select f).ToList();
            FeedPost feedpost = new FeedPost();
            if (list.Count > 0) feedpost = list[0];
            feedpost.IsUpcoming = !feedpost.IsUpcoming;
            connection.InsertOrUpdate(feedpost);
            var data = connection.GetAll<FeedPost>();
            return PartialView(data);

        }

        [HttpPost]
        public ActionResult GenerateFeeds()
        {
            GetFeedsService.GetRSSFeeds();
            return RedirectToAction("FeedPost");
        }

        public ActionResult ExtractSource(int id)
        {
            GetFeedsService.GetRssFeedBySource(id);
            return RedirectToAction("SourceDetail");
        }

        public ActionResult ExtractSourceUrl(int id)
        {
            GetFeedsService.GetRssFeedBySourceUrl(id);
            return RedirectToAction("SourceDetail");
        }
        


        //public ActionResult GetFeeds(string url)
        //{
        //    var sourcedetailconnection = new RepositoryBaseMongo("SourceDetail");
        //    var list = sourcedetailconnection.GetAll<SourceDetail>();
        //    var feedConnection = new RepositoryBaseMongo("FeedPost");
        //    var data = feedConnection.GetAll<FeedPost>().Select(x => x.SourceLink).ToList();
        //    foreach (var item in list)
        //    {
        //        var rssFeeds = RssReader.GetRssFeed(item.FeedURL);
        //        foreach (var rssFeed in rssFeeds.Where(x => !data.Contains(x.Link)))
        //        {
        //            var connection = new RepositoryBaseMongo("FeedPost");
        //            var feed = new FeedPost()
        //            {
        //                Title = rssFeed.Title,
        //                ShortDescription = rssFeed.Description,
        //                FullDescription = rssFeed.Description,
        //                SourceId = item.SourceId,
        //                CategoryId = item.CategoryId,
        //                PublishDate = rssFeed.PublishDate,
        //                SourceLink = rssFeed.Link,
        //                ImageURL1 = rssFeed.ImageUrl,
        //                IsActive = false
        //            };
        //            var imageUrl = UtilityService.GetImageUrlFromSite(rssFeed.Link);
        //            if (imageUrl != null)
        //            {
        //                feed.ImageURL1 = imageUrl;
        //            }
        //            else if (feed.ImageURL1 == null)
        //            {
        //                feed.ImageURL1 = "../../Images/NoImage.jpg";
        //            }
        //            int Id = 0;
        //            var feedlist = connection.GetAll<FeedPost>().ToList();
        //            if (feedlist.Any())
        //                Id = feedlist.Max(x => x.FeedPostId);
        //            Id += 1;
        //            feed.FeedPostId = Id;
        //            connection.InsertOrUpdate(feed);
        //        }
        //    }
        //    return RedirectToAction("FeedPost");
        //}

        #endregion

        #region Industries
        public ActionResult Industries()
        {
            var connection = new RepositoryBaseMongo("Industries");
            var data = connection.GetAll<Industries>();
            return View(data);
        }

        public ActionResult IndustriesAdd()
        {
            var connection = new RepositoryBaseMongo("Industries");
            var Industries = new Industries();
            Industries.IndustriesList = new List<SelectListItem>();
            var data = connection.GetAll<Industries>();
            //Industries.IndustriesList = new SelectList(data, "IndustriesId", "Description").ToList();
            //Industries.IndustriesList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(Industries);
        }
        [HttpPost]
        public ActionResult IndustriesAdd(Industries Industries)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Industries");
                int Id = 0;
                var Industriess = connection.GetAll<Industries>().ToList();
                if (Industriess.Any())
                    Id = Industriess.Max(x => x.IndustriesId);
                Id += 1;
                Industries.IndustriesId = Id;
                Industries.IsActive = true;
                connection.InsertOrUpdate(Industries);
                return RedirectToAction("Industries");
            }
            return null;
        }

        public ActionResult IndustriesEdit(int id)
        {
            var connection = new RepositoryBaseMongo("Industries");
            List<Industries> list = (from f in connection.GetAll<Industries>()
                                     where f.IndustriesId == id
                                     select f).ToList();
            Industries Industries = new Industries();
            if (list.Count > 0) Industries = list[0];
            var data = connection.GetAll<Industries>();
            Industries.IndustriesList = new SelectList(data, "IndustriesId", "Description").ToList();
            Industries.IndustriesList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(Industries);
        }

        [HttpPost]
        public ActionResult IndustriesEdit(Industries Industries)
        {
            var connection = new RepositoryBaseMongo("Industries");
            List<Industries> list = (from f in connection.GetAll<Industries>()
                                   where f.IndustriesId == Industries.IndustriesId
                                   select f).ToList();
            Industries newindustry = new Industries();
            if (list.Count > 0) newindustry = list[0];
            Industries._id = newindustry._id;
            connection.InsertOrUpdate(Industries);
            return RedirectToAction("Industries");
        }

        public ActionResult IndustriesDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Industries");
            List<Industries> list = (from f in connection.GetAll<Industries>()
                                   where f.IndustriesId == id
                                   select f).ToList();
            Industries industry = new Industries();
            if (list.Count > 0) industry = list[0];
            connection.Delete<Industries>(industry._id);
            return RedirectToAction(string.Format("Indu{0}stries", "ARG0")); 
        }
        



        #endregion

        #region Country
        public ActionResult Country()
        {
            var connection = new RepositoryBaseMongo("Country");
            var data = connection.GetAll<Country>();
            return View(data);
        }
        public ActionResult Countryadd()
        {

            var connection = new RepositoryBaseMongo("Country");
            var country = new Country();
            country.CountryList = new List<SelectListItem>();
            var data = connection.GetAll<Country>();
            country.CountryList = new SelectList(data, "CountryID", "Title").ToList();
            country.CountryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(country);
        }
        [HttpPost]
        public ActionResult Countryadd(Country country)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Country");
                int Id = 0;
                var countries = connection.GetAll<Country>().ToList();
                if (countries.Any())
                    Id = countries.Max(x => x.CountryID);
                Id += 1;
                country.CountryID = Id;
                country.isActive = true;
                connection.InsertOrUpdate(country);
                return RedirectToAction("Country");
            }

            return null;
        }

        public ActionResult CountryEdit(int id)
        {

            var connection = new RepositoryBaseMongo("Country");
            List<Country> list = (from f in connection.GetAll<Country>()
                                   where f.CountryID == id
                                   select f).ToList();
            Country country = new Country();
            if (list.Count > 0) country = list[0];
            var data = connection.GetAll<Country>();
            country.CountryList = new SelectList(data, "CountryId", "Title").ToList();
            country.CountryList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(country);
        }
        [HttpPost]
        public ActionResult CountryEdit(Country country)
        {
            var connection = new RepositoryBaseMongo("Country");
            List<Country> list = (from f in connection.GetAll<Country>()
                                  where f.CountryID == country.CountryID
                                   select f).ToList();
            Country newcountry = new Country();
            if (list.Count > 0) newcountry = list[0];
            country._id = newcountry._id;
            connection.InsertOrUpdate(country);
            return RedirectToAction("Country");
        }
        public ActionResult CountryDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Country");
            List<Country> list = (from f in connection.GetAll<Country>()
                                   where f.CountryID == id
                                   select f).ToList();
            Country country = new Country();
            if (list.Count > 0) country = list[0];
            connection.Delete<Country>(country._id);
            return RedirectToAction("Country");
        }

        #endregion

        #region State
        public ActionResult State()
        {

            var connection = new RepositoryBaseMongo("State");
            var data = connection.GetAll<State>();
            return View(data);

           
        }
        public ActionResult StateAdd()
        {
            
            var state = new State();
            state.StateList = new List<SelectListItem>();
            var connectioncountry = new RepositoryBaseMongo("Country");
            var data = connectioncountry.GetAll<Country>();
            state.StateList = new SelectList(data, "CountryId", "Title").ToList();
            state.StateList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
           
            return View(state);
        }
        [HttpPost]
        public ActionResult StateAdd(State state)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("State");
                int Id = 0;
                var states = connection.GetAll<State>().ToList();
                if (states.Any())
                    Id = states.Max(x => x.StateId);
                Id += 1;
                state.StateId = Id;
                state.isActive = true;
                connection.InsertOrUpdate(state);
                return RedirectToAction("State");
            }

            return null;
        }
        public ActionResult StateEdit(int id)
        {
            
            var connection = new RepositoryBaseMongo("State");
            List<State> list = (from f in connection.GetAll<State>()
                where f.StateId == id
                select f).ToList();
            State state = new State();
            
            if (list.Count > 0) state = list[0];
            var conn = new RepositoryBaseMongo("Country");

            var data = conn.GetAll<Country>();
            state.StateList = new SelectList(data, "CountryId", "Title").ToList();
            state.StateList.Insert(0, (new SelectListItem {Text = "None", Value = "0"}));
            return View(state);
        }


        [HttpPost]
        public ActionResult StateEdit(State state)
        {
            var connection = new RepositoryBaseMongo("State");
            List<State> list = (from f in connection.GetAll<State>()
                                where f.StateId == state.StateId
                                   select f).ToList();
            State newstate = new State();
            if (list.Count > 0) newstate = list[0];
            state._id = newstate._id;
            connection.InsertOrUpdate(state);
            return RedirectToAction("State");
        }
        public ActionResult StateDelete(int id)
        {
            var connection = new RepositoryBaseMongo("State");
            List<State> list = (from f in connection.GetAll<State>()
                                   where f.StateId == id
                                   select f).ToList();
            State state = new State();
            if (list.Count > 0) state = list[0];
            connection.Delete<State>(state._id);
            return RedirectToAction("State");
        }
        #endregion

        #region Occupation
        public ActionResult Occupation()
        {
            var con = new RepositoryBaseMongo("Occupation");
            var data = con.GetAll<Occupation>();
            return View(data);
        }
        public ActionResult OccupationAdd()
        {
            var con = new RepositoryBaseMongo("Occupation");
            var occupation = new Occupation();
            occupation.OccupationList = new List<SelectListItem>();
            var data = con.GetAll<Occupation>();
            occupation.OccupationList = new SelectList(data, "OccupationId", "Description").ToList();
            occupation.OccupationList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(occupation);
        }


        [HttpPost]
        public ActionResult OccupationAdd(Occupation occupation)
        {

            var connection = new RepositoryBaseMongo("Occupation");
            int Id = 0;
            var occupations = connection.GetAll<Occupation>().ToList();
            if (occupations.Any())
                Id = occupations.Max(x => x.OccupationId);
            Id += 1;
            occupation.OccupationId = Id;
            occupation.IsActive = true;
            connection.InsertOrUpdate(occupation);
            return RedirectToAction("Occupation");



        }

        public ActionResult OccupationEdit(int Id)
        {
            var con = new RepositoryBaseMongo("Occupation");
            List<Occupation> list = (from f in con.GetAll<Occupation>()
                                     where f.OccupationId == Id
                                     select f).ToList();
            Occupation occupations = new Occupation();
            if (list.Count > 0) occupations = list[0];
            return View(occupations);
        }
        [HttpPost]
        public ActionResult OccupationEdit(Occupation occupation)
        {
            var con = new RepositoryBaseMongo("Occupation");
            List<Occupation> list = (from f in con.GetAll<Occupation>()
                                     where f.OccupationId == occupation.OccupationId
                                     select f).ToList();
            Occupation Ocupations = new Occupation();
            if (list.Count > 0) Ocupations = list[0];
            occupation._id = Ocupations._id;
            con.InsertOrUpdate(occupation);
            return RedirectToAction("Occupation");
        }
        public ActionResult OccupationDelete(int Id)
        {
            var con = new RepositoryBaseMongo("Occupation");
            List<Occupation> list = (from f in con.GetAll<Occupation>()
                                     where f.OccupationId == Id
                                     select f).ToList();
            Occupation occupation = new Occupation();
            if (list.Count > 0) occupation = list[0];
            con.Delete<Occupation>(occupation._id);
            return RedirectToAction("Occupation");

        }
        #endregion

        #region EmailTemplate
        public ActionResult EmailTemplate()
        {
            var connection = new RepositoryBaseMongo("EmailTemplate");
            var data = connection.GetAll<EmailTemplate>();
            return View(data);

        }
        public ActionResult EmailTemplateAdd()
        {
            var con = new RepositoryBaseMongo("EmailTemplate");
            var email = new EmailTemplate();
            email.EmailTemplateList = new List<SelectListItem>();
            var data = con.GetAll<EmailTemplate>();
            email.EmailTemplateList = new SelectList(data, "EmailId", "Description").ToList();
            email.EmailTemplateList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
            return View(email);

        }
        [HttpPost]
        public ActionResult EmailTemplateAdd(EmailTemplate Email)
        {
            var con = new RepositoryBaseMongo("EmailTemplate");
            int Id = 0;
            var emails = con.GetAll<EmailTemplate>().ToList();
            if (emails.Any())
                Id = emails.Max(x => x.EmailId);
            Id += 1;
            Email.EmailId = Id;
            Email.IsActive = true;
            con.InsertOrUpdate(Email);
            return RedirectToAction("EmailTemplate");
        }
        public ActionResult EmailTemplateEdit(int Id)
        {
            var con = new RepositoryBaseMongo("EmailTemplate");
            List<EmailTemplate> list = (from f in con.GetAll<EmailTemplate>()
                                        where f.EmailId == Id
                                        select f).ToList();
            EmailTemplate emails = new EmailTemplate();
            if (list.Count > 0) emails = list[0];
            return View(emails);

        }
        [HttpPost]
        public ActionResult EmailTemplateEdit(EmailTemplate Email)
        {
            var con = new RepositoryBaseMongo("EmailTemplate");
            List<EmailTemplate> list = (from f in con.GetAll<EmailTemplate>()
                                        where f.EmailId == Email.EmailId
                                        select f).ToList();
            EmailTemplate emails = new EmailTemplate();
            if (list.Count > 0) emails = list[0];
            Email._id = emails._id;
            con.InsertOrUpdate(Email);
            return RedirectToAction("EmailTemplate");
        }
        public ActionResult EmailTemplateDelete(int Id)
        {
            var con = new RepositoryBaseMongo("EmailTemplate");
            List<EmailTemplate> list = (from f in con.GetAll<EmailTemplate>()
                                        where f.EmailId == Id
                                        select f).ToList();
            EmailTemplate email = new EmailTemplate();
            if (list.Count > 0) email = list[0];
            con.Delete<EmailTemplate>(email._id);
            return RedirectToAction("EmailTemplate");

        }


        #endregion

        #region Users
        public ActionResult Users()
        {
            var connection = new RepositoryBaseMongo("Users");
            var data = connection.GetAll<Users>();
            return View(data);
        }

        //public ActionResult UserAdd()
        //{
        //    var connection = new RepositoryBaseMongo("Users");
        //    var category = new Users();
        //    return View(category);
        
        //}

        //[HttpPost]
        //public ActionResult UserAdd(Users users, string dob_month, string dob_day, string dob_year)
        //{
        //    var connection = new RepositoryBaseMongo("Users");
        //    int Id = 0;


        //    var userList= connection.GetAll<Users>().ToList();
        //    if (userList.Any())
        //        Id = userList.Max(x => x.UserId);
        //    Id += 1;
        //    users.UserId = Id;
        //    var dob =dob_month + "/" + dob_day + "/" + dob_year;
        //    users.BirthDay = Convert.ToDateTime(dob).AddDays(1);
        //    connection.InsertOrUpdate(users);
        //    return RedirectToAction("Users");
        //}

        public ActionResult UserEdit(int id)
        {
            var connection = new RepositoryBaseMongo("Users");
            List<Users> list = (from f in connection.GetAll<Users>()
                                   where f.UserId == id
                                   select f).ToList();
            Users users = new Users();
            if (list.Count > 0) users = list[0];

            var doblist = users.BirthDay.ToShortDateString().Split('/');
                ViewBag.Month = doblist[0];
                ViewBag.day = doblist[1];
                ViewBag.Year = doblist[2]; 
 
            return View(users);
        }


        [HttpPost]
        public ActionResult UserEdit(Users user, string dob_month, string dob_day, string dob_year)
        {
            var connection = new RepositoryBaseMongo("Users");
            List<Users> list = (from f in connection.GetAll<Users>()
                                   where f.UserId == user.UserId
                                   select f).ToList();
            Users newcategory = new Users();
            if (list.Count > 0) newcategory = list[0];
            user.UserId = newcategory.UserId;
            var dob = dob_month + "/" + dob_day + "/" + dob_year;
            user.BirthDay = Convert.ToDateTime(dob).AddDays(1);
            connection.InsertOrUpdate(user);
            return RedirectToAction("Users");
        }

        public ActionResult UserDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Users");
            connection.Delete<Users>(id);
            return RedirectToAction("Users");
        }

        #endregion

        #region Tags
        public ActionResult Tags()
        {
            var con = new RepositoryBaseMongo("Tags");
            var data = con.GetAll<Tags>();
            return View(data);
            
        }
        public ActionResult AddTags()
        {
            var connection = new RepositoryBaseMongo("Tags");
            var tag = new Tags();
            return View(tag);
        }

        [HttpPost]
        public ActionResult AddTags(Tags tag)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Tags");
                int Id = 0;
                var tag1 = connection.GetAll<Tags>().ToList();
                if (tag1.Any())
                    Id = tag1.Max(x => x.TagId);
                Id += 1;
                tag.TagId = Id;
                connection.InsertOrUpdate(tag);
                return RedirectToAction("Tags");
            }
            return null;
        }


            public ActionResult EditTags(int id)
        {

            var connection = new RepositoryBaseMongo("Tags");
            List<Tags> list = (from f in connection.GetAll<Tags>()
                                 where f.TagId == id
                                 select f).ToList();
            Tags tags = new Tags();
            if (list.Count > 0) tags = list[0];

            return View(tags);
        }

        [HttpPost]
        public ActionResult EditTags(Tags tag)
        {
            var connection = new RepositoryBaseMongo("Tags");
            List<Tags> list = (from f in connection.GetAll<Tags>()
                                 where f.TagId == tag.TagId
                                 select f).ToList();
            Tags tagss = new Tags();
            if (list.Count > 0) tagss = list[0];
            tag._id = tagss._id;
            connection.InsertOrUpdate(tag);
            return RedirectToAction("Tags");
        }

        public ActionResult DeleteTags(int id)
        {
            var connection = new RepositoryBaseMongo("Tags");
            List<Tags> list = (from f in connection.GetAll<Tags>()
                                 where f.TagId == id
                                 select f).ToList();
            Tags tag = new Tags();
            if (list.Count > 0) tag = list[0];
            connection.Delete<Tags>(tag._id);
            return RedirectToAction("Tags");
        }

        #endregion

        # region ProductServiceCategory

        public ActionResult ProductServiceCategory()
        {
            var connection = new RepositoryBaseMongo("ProductServiceCategory");
            var data = connection.GetAll<ProductServiceCategory>();
            return View(data);
        }

        public ActionResult ProductServiceCategoryAdd()
        {
            var company = new ProductServiceCategory();
            return View(company);
        }

        [HttpPost]
        public ActionResult ProductServiceCategoryAdd(ProductServiceCategory model)
        {
            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("ProductServiceCategory");
                int id = 0;
                var data = connection.GetAll<ProductServiceCategory>().ToList();
                if (data.Any())
                    id = data.Max(x => x.ProductServiceCategoryId);
                id += 1;
                model.ProductServiceCategoryId = id;
                model.IsActive = true;
                connection.InsertOrUpdate(model);
                return RedirectToAction("ProductServiceCategory");
            }
            return null;
        }

        public ActionResult ProductServiceCategoryEdit(int id)
        {
            var connection = new RepositoryBaseMongo("ProductServiceCategory");
            List<ProductServiceCategory> list = (from f in connection.GetAll<ProductServiceCategory>()
                where f.ProductServiceCategoryId == id
                select f).ToList();
            ProductServiceCategory category = new ProductServiceCategory();
            if (list.Count > 0) category = list[0];
            //var data = connection.GetAll<ProductServiceCategory>();
            return View(category);
        }

        [HttpPost]
        public ActionResult ProductServiceCategoryEdit(ProductServiceCategory category)
        {
            var connection = new RepositoryBaseMongo("ProductServiceCategory");
            List<ProductServiceCategory> list = (from f in connection.GetAll<ProductServiceCategory>()
                where f.ProductServiceCategoryId == category.ProductServiceCategoryId
                select f).ToList();
            ProductServiceCategory newCategory = new ProductServiceCategory();
            if(list.Count > 0) newCategory = list[0];
            category._id = newCategory._id;
            connection.InsertOrUpdate(category);
            return RedirectToAction("ProductServiceCategory");
        }

        public ActionResult ProductServiceCategoryDelete(int id)
        {
            var connection = new RepositoryBaseMongo("ProductServiceCategory");
            List<ProductServiceCategory> list = (from f in connection.GetAll<ProductServiceCategory>()
                where f.ProductServiceCategoryId == id
                select f).ToList();
            ProductServiceCategory category = new ProductServiceCategory();
            if (list.Count > 0) category = list[0];
            connection.Delete<ProductServiceCategory>(category._id);
            return RedirectToAction("ProductServiceCategory");
        }

        # endregion ProductServiceCategory

        #region Ethnicity
        public ActionResult Ethnicity()
        { 
        var con = new RepositoryBaseMongo("Ethnicity");
        var list = con.GetAll<Ethnicity>();
        return View(list);
        }


        public ActionResult EthnicityAdd()
        {
            var con = new RepositoryBaseMongo("Ethnicity");
            var eth = new Ethnicity();
            return View(eth);
        }
        [HttpPost]
        public ActionResult EthnicityAdd(Ethnicity eth)
        {

            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("Ethnicity");
                int Id = 0;
                var eth1 = connection.GetAll<Ethnicity>().ToList();
                if (eth1.Any())
                    Id = eth1.Max(x => x.EthnicityId);
                Id += 1;
                eth.EthnicityId = Id;
                connection.InsertOrUpdate(eth);
                return RedirectToAction("Ethnicity");
            }
            return null;
        }
          public ActionResult EthnicityEdit(int id)
        {

            var connection = new RepositoryBaseMongo("Ethnicity");
            List<Ethnicity> list = (from f in connection.GetAll<Ethnicity>()
                                 where f.EthnicityId == id
                                 select f).ToList();
            Ethnicity eth = new Ethnicity();
            if (list.Count > 0) eth = list[0];

            return View(eth);
        }
        
        [HttpPost]
        public ActionResult EthnicityEdit(Ethnicity eth)
        {
            var connection = new RepositoryBaseMongo("Ethnicity");
            List<Ethnicity> list = (from f in connection.GetAll<Ethnicity>()
                                 where f.EthnicityId == eth.EthnicityId
                                 select f).ToList();
            Ethnicity eth1 = new Ethnicity();
            if (list.Count > 0) eth1 = list[0];
            eth._id = eth1._id;
            connection.InsertOrUpdate(eth);
            return RedirectToAction("Ethnicity");
        }

         public ActionResult EthnicityDelete(int id)
        {
            var connection = new RepositoryBaseMongo("Ethnicity");
            List<Ethnicity> list = (from f in connection.GetAll<Ethnicity>()
                                 where f.EthnicityId == id
                                 select f).ToList();
            Ethnicity eth1 = new Ethnicity();
            if (list.Count > 0) eth1 = list[0];
            connection.Delete<Ethnicity>(eth1._id);
            return RedirectToAction("Ethnicity");
        }



  
        #endregion Ethnicity


         #region BodyType
         public ActionResult BodyType()
        { 
        var con = new RepositoryBaseMongo("BodyType");
        var list = con.GetAll<BodyType>();
        return View(list);
        }


        public ActionResult BodyTypeAdd()
        {
            var con = new RepositoryBaseMongo("BodyType");
            var body = new BodyType();
            return View(body);
        }
        [HttpPost]
        public ActionResult BodyTypeAdd(BodyType body)
        {

            if (ModelState.IsValid)
            {
                var connection = new RepositoryBaseMongo("BodyType");
                int Id = 0;
                var bodyT = connection.GetAll<BodyType>().ToList();
                if (bodyT.Any())
                    Id = bodyT.Max(x => x.BodyTypeId);
                Id += 1;
                body.BodyTypeId = Id;
                connection.InsertOrUpdate(body);
                return RedirectToAction("BodyType");
            }
            return null;
        }
          public ActionResult BodyTypeEdit(int id)
        {

            var connection = new RepositoryBaseMongo("BodyType");
            List<BodyType> list = (from f in connection.GetAll<BodyType>()
                                 where f.BodyTypeId == id
                                 select f).ToList();
            BodyType bodyT = new BodyType();
            if (list.Count > 0) bodyT = list[0];

            return View(bodyT);
        }
        
        [HttpPost]
          public ActionResult BodyTypeEdit(BodyType body)
        {
            var connection = new RepositoryBaseMongo("BodyType");
            List<BodyType> list = (from f in connection.GetAll<BodyType>()
                                 where f.BodyTypeId == body.BodyTypeId
                                 select f).ToList();
            BodyType bodyT = new BodyType();
            if (list.Count > 0) bodyT = list[0];
            body._id = bodyT._id;
            connection.InsertOrUpdate(body);
            return RedirectToAction("BodyType");
        }

         public ActionResult BodyTypeDelete(int id)
        {
            var connection = new RepositoryBaseMongo("BodyType");
            List<BodyType> list = (from f in connection.GetAll<BodyType>()
                                 where f.BodyTypeId == id
                                 select f).ToList();
            BodyType bodyT = new BodyType();
            if (list.Count > 0) bodyT = list[0];
            connection.Delete<BodyType>(bodyT._id);
            return RedirectToAction("BodyType");
        }
#endregion BodyType

         #region EyeColor
         public ActionResult EyeColor()
         {
             var con = new RepositoryBaseMongo("EyeColor");
             var list = con.GetAll<EyeColor>();
             return View(list);
         }


         public ActionResult EyeColorAdd()
         {
             var con = new RepositoryBaseMongo("EyeColor");
             var eye = new EyeColor();
             return View(eye);
         }
         [HttpPost]
         public ActionResult EyeColorAdd(EyeColor eye)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("EyeColor");
                 int Id = 0;
                 var eyeT = connection.GetAll<EyeColor>().ToList();
                 if (eyeT.Any())
                     Id = eyeT.Max(x => x.EyeColorId);
                 Id += 1;
                 eye.EyeColorId = Id;
                 connection.InsertOrUpdate(eye);
                 return RedirectToAction("EyeColor");
             }
             return null;
         }
         public ActionResult EyeColorEdit(int id)
         {

             var connection = new RepositoryBaseMongo("EyeColor");
             List<EyeColor> list = (from f in connection.GetAll<EyeColor>()
                                    where f.EyeColorId == id
                                    select f).ToList();
             EyeColor eyeT = new EyeColor();
             if (list.Count > 0) eyeT = list[0];

             return View(eyeT);
         }

         [HttpPost]
         public ActionResult EyeColorEdit(EyeColor eye)
         {
             var connection = new RepositoryBaseMongo("EyeColor");
             List<EyeColor> list = (from f in connection.GetAll<EyeColor>()
                                    where f.EyeColorId == eye.EyeColorId
                                    select f).ToList();
             EyeColor eyeT = new EyeColor();
             if (list.Count > 0) eyeT = list[0];
             eye._id = eyeT._id;
             connection.InsertOrUpdate(eye);
             return RedirectToAction("EyeColor");
         }

         public ActionResult EyeColorDelete(int id)
         {
             var connection = new RepositoryBaseMongo("EyeColor");
             List<EyeColor> list = (from f in connection.GetAll<EyeColor>()
                                    where f.EyeColorId == id
                                    select f).ToList();
             EyeColor eyeT = new EyeColor();
             if (list.Count > 0) eyeT = list[0];
             connection.Delete<EyeColor>(eyeT._id);
             return RedirectToAction("EyeColor");
         }
         #endregion EyeColor

         #region Religion
         public ActionResult Religion()
         {
             var con = new RepositoryBaseMongo("Religion");
             var list = con.GetAll<Religion>();
             return View(list);
         }


         public ActionResult ReligionAdd()
         {
             var con = new RepositoryBaseMongo("Religion");
             var rel = new Religion();
             return View(rel);
         }
         [HttpPost]
         public ActionResult ReligionAdd(Religion per)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("Religion");
                 int Id = 0;
                 var relN = connection.GetAll<Religion>().ToList();
                 if (relN.Any())
                     Id = relN.Max(x => x.ReligionId);
                 Id += 1;
                 per.ReligionId = Id;
                 connection.InsertOrUpdate(per);
                 return RedirectToAction("Religion");
             }
             return null;
         }
         public ActionResult ReligionEdit(int id)
         {

             var connection = new RepositoryBaseMongo("Religion");
             List<Religion> list = (from f in connection.GetAll<Religion>()
                                    where f.ReligionId == id
                                    select f).ToList();
             Religion relN = new Religion();
             if (list.Count > 0) relN = list[0];

             return View(relN);
         }

         [HttpPost]
         public ActionResult ReligionEdit(Religion rel)
         {
             var connection = new RepositoryBaseMongo("Religion");
             List<Religion> list = (from f in connection.GetAll<Religion>()
                                    where f.ReligionId == rel.ReligionId
                                    select f).ToList();
             Religion relN = new Religion();
             if (list.Count > 0) relN = list[0];
             rel._id = relN._id;
             connection.InsertOrUpdate(rel);
             return RedirectToAction("Religion");
         }

         public ActionResult ReligionDelete(int id)
         {
             var connection = new RepositoryBaseMongo("Religion");
             List<Religion> list = (from f in connection.GetAll<Religion>()
                                    where f.ReligionId == id
                                    select f).ToList();
             Religion relN = new Religion();
             if (list.Count > 0) relN = list[0];
             connection.Delete<Religion>(relN._id);
             return RedirectToAction("Religion");
         }
         #endregion Religion

         #region SmokingHabits
         public ActionResult SmokingHabits()
         {
             var con = new RepositoryBaseMongo("SmokingHabits");
             var list = con.GetAll<SmokingHabits>();
             return View(list);
         }


         public ActionResult SmokingHabitsAdd()
         {
             var con = new RepositoryBaseMongo("SmokingHabits");
             var smk = new SmokingHabits();
             return View(smk);
         }
         [HttpPost]
         public ActionResult SmokingHabits(SmokingHabits smk)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("SmokingHabits");
                 int Id = 0;
                 var smkH = connection.GetAll<SmokingHabits>().ToList();
                 if (smkH.Any())
                     Id = smkH.Max(x => x.SmokingHabitsId);
                 Id += 1;
                 smk.SmokingHabitsId = Id;
                 connection.InsertOrUpdate(smk);
                 return RedirectToAction("SmokingHabits");
             }
             return null;
         }
         public ActionResult SmokingHabitsEdit(int id)
         {

             var connection = new RepositoryBaseMongo("SmokingHabits");
             List<SmokingHabits> list = (from f in connection.GetAll<SmokingHabits>()
                                         where f.SmokingHabitsId == id
                                    select f).ToList();
             SmokingHabits smkH = new SmokingHabits();
             if (list.Count > 0) smkH = list[0];

             return View(smkH);
         }

         [HttpPost]
         public ActionResult SmokingHabitsEdit(SmokingHabits smk)
         {
             var connection = new RepositoryBaseMongo("SmokingHabits");
             List<SmokingHabits> list = (from f in connection.GetAll<SmokingHabits>()
                                         where f.SmokingHabitsId == smk.SmokingHabitsId
                                    select f).ToList();
             SmokingHabits smkH = new SmokingHabits();
             if (list.Count > 0) smkH = list[0];
             smk._id= smkH._id;
             connection.InsertOrUpdate(smk);
             return RedirectToAction("SmokingHabits");
         }

         public ActionResult SmokingHabitsDelete(int id)
         {
             var connection = new RepositoryBaseMongo("SmokingHabits");
             List<SmokingHabits> list = (from f in connection.GetAll<SmokingHabits>()
                                         where f.SmokingHabitsId == id
                                    select f).ToList();
             SmokingHabits SmkH = new SmokingHabits();
             if (list.Count > 0) SmkH = list[0];
             connection.Delete<SmokingHabits>(SmkH._id);
             return RedirectToAction("SmokingHabits");
         }
         #endregion SmokingHabits

         #region CurrentlyLivingWith
         public ActionResult CurrentlyLivingWith()
         {
             var con = new RepositoryBaseMongo("CurrentlyLivingWith");
             var list = con.GetAll<CurrentlyLivingWith>();
             return View(list);
         }


         public ActionResult CurrentlyLivingWithAdd()
         {
             var con = new RepositoryBaseMongo("CurrentlyLivingWith");
             var clw = new CurrentlyLivingWith();
             return View(clw);
         }
         [HttpPost]
         public ActionResult CurrentlyLivingWithAdd(CurrentlyLivingWith clw)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("CurrentlyLivingWith");
                 int Id = 0;
                 var clw1 = connection.GetAll<CurrentlyLivingWith>().ToList();
                 if (clw1.Any())
                     Id = clw1.Max(x => x.CurrentlyLivingWithId);
                 Id += 1;
                 clw.CurrentlyLivingWithId = Id;
                 connection.InsertOrUpdate(clw);
                 return RedirectToAction("CurrentlyLivingWith");
             }
             return null;
         }
         public ActionResult CurrentlyLivingWithEdit(int id)
         {

             var connection = new RepositoryBaseMongo("CurrentlyLivingWith");
             List<CurrentlyLivingWith> list = (from f in connection.GetAll<CurrentlyLivingWith>()
                                               where f.CurrentlyLivingWithId == id
                                               select f).ToList();
             CurrentlyLivingWith clw1 = new CurrentlyLivingWith();
             if (list.Count > 0) clw1 = list[0];

             return View(clw1);
         }

         [HttpPost]
         public ActionResult CurrentlyLivingWithEdit(CurrentlyLivingWith clw)
         {
             var connection = new RepositoryBaseMongo("CurrentlyLivingWith");
             List<CurrentlyLivingWith> list = (from f in connection.GetAll<CurrentlyLivingWith>()
                                               where f.CurrentlyLivingWithId == clw.CurrentlyLivingWithId
                                               select f).ToList();
             CurrentlyLivingWith clw1 = new CurrentlyLivingWith();
             if (list.Count > 0) clw1 = list[0];
             clw._id = clw1._id;
             connection.InsertOrUpdate(clw);
             return RedirectToAction("CurrentlyLivingWith");
         }

         public ActionResult CurrentlyLivingWithDelete(int id)
         {
             var connection = new RepositoryBaseMongo("CurrentlyLivingWith");
             List<CurrentlyLivingWith> list = (from f in connection.GetAll<CurrentlyLivingWith>()
                                               where f.CurrentlyLivingWithId == id
                                               select f).ToList();
             CurrentlyLivingWith clw1 = new CurrentlyLivingWith();
             if (list.Count > 0) clw1 = list[0];
             connection.Delete<CurrentlyLivingWith>(clw1._id);
             return RedirectToAction("CurrentlyLivingWith");
         }
         #endregion CurrentlyLivingWith

         #region DrinkingHabits
         public ActionResult DrinkingHabits()
         {
             var con = new RepositoryBaseMongo("DrinkingHabits");
             var list = con.GetAll<DrinkingHabits>();
             return View(list);
         }


         public ActionResult DrinkingHabitsAdd()
         {
             var con = new RepositoryBaseMongo("DrinkingHabits");
             var drnk = new DrinkingHabits();
             return View(drnk);
         }
         [HttpPost]
         public ActionResult DrinkingHabitsAdd(DrinkingHabits drnk)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("DrinkingHabits");
                 int Id = 0;
                 var drnkH = connection.GetAll<DrinkingHabits>().ToList();
                 if (drnkH.Any())
                     Id = drnkH.Max(x => x.DrinkingHabitsId);
                 Id += 1;
                 drnk.DrinkingHabitsId = Id;
                 connection.InsertOrUpdate(drnk);
                 return RedirectToAction("DrinkingHabits");
             }
             return null;
         }
         public ActionResult DrinkingHabitsEdit(int id)
         {

             var connection = new RepositoryBaseMongo("DrinkingHabits");
             List<DrinkingHabits> list = (from f in connection.GetAll<DrinkingHabits>()
                                          where f.DrinkingHabitsId == id
                                          select f).ToList();
             DrinkingHabits drnkH = new DrinkingHabits();
             if (list.Count > 0) drnkH = list[0];

             return View(drnkH);
         }

         [HttpPost]
         public ActionResult DrinkingHabitsEdit(DrinkingHabits drnk)
         {
             var connection = new RepositoryBaseMongo("DrinkingHabits");
             List<DrinkingHabits> list = (from f in connection.GetAll<DrinkingHabits>()
                                          where f.DrinkingHabitsId == drnk.DrinkingHabitsId
                                          select f).ToList();
             DrinkingHabits drnkH = new DrinkingHabits();
             if (list.Count > 0) drnkH = list[0];
             drnk._id = drnkH._id;
             connection.InsertOrUpdate(drnk);
             return RedirectToAction("DrinkingHabits");
         }

         public ActionResult DrinkingHabitsDelete(int id)
         {
             var connection = new RepositoryBaseMongo("DrinkingHabits");
             List<DrinkingHabits> list = (from f in connection.GetAll<DrinkingHabits>()
                                          where f.DrinkingHabitsId == id
                                          select f).ToList();
             DrinkingHabits drnkH = new DrinkingHabits();
             if (list.Count > 0) drnkH = list[0];
             connection.Delete<DrinkingHabits>(drnkH._id);
             return RedirectToAction("DrinkingHabits");
         }
         #endregion DrinkingHabits

         #region JobCategory
         public ActionResult JobCategory()
         {
             var conn = new RepositoryBaseMongo("JobCategory");
             var data = conn.GetAll<JobCategory>();
             return View(data);
         }
         public ActionResult JobCategoryAdd()
         {
             var category = new JobCategory();


             return View(category);
         }
         [HttpPost]
         public ActionResult JobCategoryAdd(JobCategory model)
         {
             var connection = new RepositoryBaseMongo("JobCategory");
             int Id = 0;
             var category = connection.GetAll<JobCategory>().ToList();
             if (category.Any())
                 Id = category.Max(x => x.JobCategoryId);
             Id += 1;
             model.JobCategoryId = Id;
             model.isActive = true;
             connection.InsertOrUpdate(model);
             return RedirectToAction("JobCategory");

         }
         public ActionResult JobCategoryEdit(int id)
         {
             var connection = new RepositoryBaseMongo("JobCategory");
             List<JobCategory> list = (from f in connection.GetAll<JobCategory>()
                                       where f.JobCategoryId == id
                                       select f).ToList();
             JobCategory category = new JobCategory();
             if (list.Count > 0) category = list[0];
             return View(category);
         }
         [HttpPost]
         public ActionResult JobCategoryEdit(JobCategory model)
         {

             var connection = new RepositoryBaseMongo("JobCategory");
             List<JobCategory> list = (from f in connection.GetAll<JobCategory>()
                                       where f.JobCategoryId == model.JobCategoryId
                                       select f).ToList();
             JobCategory category = new JobCategory();
             if (list.Count > 0) category = list[0];
             model._id = category._id;
             connection.InsertOrUpdate(model);
             return RedirectToAction("JobCategory");
         }
         public ActionResult JobCategoryDelete(int id)
         {
             var connection = new RepositoryBaseMongo("JobCategory");
             List<JobCategory> list = (from f in connection.GetAll<JobCategory>()
                                       where f.JobCategoryId == id
                                       select f).ToList();
             JobCategory category = new JobCategory();
             if (list.Count > 0) category = list[0];
             connection.Delete<JobCategory>(category._id);
             return RedirectToAction("JobCategory");
         }
         #endregion

         #region JobFunction
         public ActionResult JobFunction()
         {
             var conn = new RepositoryBaseMongo("JobFunction");
             var list = conn.GetAll<JobFunction>();
             return View(list);
         }
         public ActionResult JobFunctionAdd()
         {
             var function = new JobFunction();
             function.JobFunctionList = new List<SelectListItem>();
             var connection = new RepositoryBaseMongo("Category");
             var data = connection.GetAll<Category>();
             function.JobFunctionList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
             function.JobFunctionList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));

             return View(function);
         }
         [HttpPost]
         public ActionResult JobFunctionAdd(JobFunction model)
         {
             var conn = new RepositoryBaseMongo("JobFunction");
             int Id = 0;
             var function = conn.GetAll<JobFunction>().ToList();
             if (function.Any())
                 Id = function.Max(x => x.JobFunctionId);
             Id += 1;
             model.JobFunctionId = Id;
             model.isActive = true;
             conn.InsertOrUpdate(model);
             return RedirectToAction("JobFunction");
         }
         public ActionResult JobFunctionEdit(int id)
         {
             var connection = new RepositoryBaseMongo("JobFunction");
             List<JobFunction> list = (from f in connection.GetAll<JobFunction>()
                                       where f.JobFunctionId == id
                                       select f).ToList();
             JobFunction function = new JobFunction();

             if (list.Count > 0) function = list[0];
             var conn = new RepositoryBaseMongo("Category");

             var data = conn.GetAll<Category>();
             function.JobFunctionList = new SelectList(data, "CategoryId", "ShortDescription").ToList();
             function.JobFunctionList.Insert(0, (new SelectListItem { Text = "None", Value = "0" }));
             return View(function);
         }
         [HttpPost]
         public ActionResult JobFunctionEdit(JobFunction model)
         {
             var connection = new RepositoryBaseMongo("JobFunction");
             List<JobFunction> list = (from f in connection.GetAll<JobFunction>()
                                       where f.JobFunctionId == model.JobFunctionId
                                       select f).ToList();
             JobFunction category = new JobFunction();
             if (list.Count > 0) category = list[0];
             model._id = category._id;
             connection.InsertOrUpdate(model);
             return RedirectToAction("JobFunction");
         }
         public ActionResult JobFunctionDelete(int id)
         {

             var connection = new RepositoryBaseMongo("JobFunction");
             List<JobFunction> list = (from f in connection.GetAll<JobFunction>()
                                       where f.JobFunctionId == id
                                       select f).ToList();
             JobFunction category = new JobFunction();
             if (list.Count > 0) category = list[0];
             connection.Delete<JobFunction>(category._id);
             return RedirectToAction("JobFunction");
         }
         #endregion

         #region Personality
         public ActionResult Personality()
         {
             var con = new RepositoryBaseMongo("Personality");
             var list = con.GetAll<Personality>();
             return View(list);
         }


         public ActionResult PersonalityAdd()
         {
             var con = new RepositoryBaseMongo("Personality");
             var per = new Personality();
             return View(per);
         }
         [HttpPost]
         public ActionResult PersonalityAdd(Personality per)
         {

             if (ModelState.IsValid)
             {
                 var connection = new RepositoryBaseMongo("Personality");
                 int Id = 0;
                 var perN = connection.GetAll<Personality>().ToList();
                 if (perN.Any())
                     Id = perN.Max(x => x.PersonalityId);
                 Id += 1;
                 per.PersonalityId = Id;
                 connection.InsertOrUpdate(per);
                 return RedirectToAction("Personality");
             }
             return null;
         }
         public ActionResult PersonalityEdit(int id)
         {

             var connection = new RepositoryBaseMongo("Personality");
             List<Personality> list = (from f in connection.GetAll<Personality>()
                                       where f.PersonalityId == id
                                    select f).ToList();
             Personality perN = new Personality();
             if (list.Count > 0) perN = list[0];

             return View(perN);
         }

         [HttpPost]
         public ActionResult PersonalityEdit(Personality rel)
         {
             var connection = new RepositoryBaseMongo("Personality");
             List<Personality> list = (from f in connection.GetAll<Personality>()
                                       where f.PersonalityId == rel.PersonalityId
                                    select f).ToList();
             Personality perN = new Personality();
             if (list.Count > 0) perN = list[0];
             rel._id = perN._id;
             connection.InsertOrUpdate(rel);
             return RedirectToAction("Personality");
         }

         public ActionResult PersonalityDelete(int id)
         {
             var connection = new RepositoryBaseMongo("Personality");
             List<Personality> list = (from f in connection.GetAll<Personality>()
                                       where f.PersonalityId == id
                                    select f).ToList();
             Personality perN = new Personality();
             if (list.Count > 0) perN = list[0];
             connection.Delete<Religion>(perN._id);
             return RedirectToAction("Personality");
         }
         #endregion Personlaity

    }
}










      